package com.zys.el.time.utils;

import com.zys.el.time.ga.Schedule;
import org.apache.poi.ss.usermodel.*;

import java.io.IOException;
import java.util.*;

public class ExcelHandle {
    public static final String[] t = {"8:00", "8:05", "8:10", "8:15", "8:20", "8:25", "8:30", "8:35", "8:40", "8:45", "8:50", "8:55", "9:00", "9:05", "9:10", "9:15", "9:20", "9:25", "9:30", "9:35", "9:40", "9:45", "9:50", "9:55",
            "10:00", "10:05", "10:10", "10:15", "10:20", "10:25", "10:30", "10:35", "10:40", "10:45", "10:50", "10:55",
            "11:00", "11:05", "11:10", "11:15", "11:20", "11:25", "11:30", "11:35", "11:40", "11:45", "11:50", "11:55",
            "13:05", "13:10", "13:15", "13:20", "13:25", "13:30", "13:35", "13:40", "13:45", "13:50", "13:55",
            "14:00", "14:05", "14:10", "14:15", "14:20", "14:25", "14:30", "14:35", "14:40", "14:45", "14:50", "14:55",
            "15:00", "15:05", "15:10", "15:15", "15:20", "15:25", "15:30", "15:35", "15:40", "15:45", "15:50", "15:55",
            "16:00", "16:05", "16:10", "16:15", "16:20", "16:25", "16:30", "16:35", "16:40", "16:45", "16:50", "16:55",
            "17:00", "17:05", "17:10", "17:15", "17:20", "17:25", "17:30", "17:35", "17:40", "17:45", "17:50", "17:55",
            "19:05", "19:10", "19:15", "19:20", "19:25", "19:30", "19:35", "19:40", "19:45", "19:50", "19:55",
            "20:00", "20:05", "20:10", "20:15", "20:20", "20:25", "20:30", "20:35", "20:40", "20:45", "20:50", "20:55", "21:00",
    };

    public static final List<String> timeList = new ArrayList<>(Arrays.asList(t));

    /**
     * 设置数据和样式
     * @param wb excel
     * @param schedules
     * @param col
     * @throws IOException
     */
    public static void exportData(Workbook wb, List<Schedule> schedules, int col) throws IOException {
        Sheet sheet = null;
        if(wb.iterator().hasNext())
            sheet = wb.getSheetAt(0);
        else
            sheet = wb.createSheet();

        setCellWithValueList(sheet, timeList, 1, 1, 1);

        Random random = new Random(System.currentTimeMillis());
        for (Schedule s : schedules) {
            List<Integer> slot = s.getSlot();
            String taskId = s.getTaskId();
            CellStyle cellStyle = wb.createCellStyle();
            cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            cellStyle.setFillForegroundColor((short) random.nextInt(65));
            setStyle(sheet, cellStyle, taskId, slot, col);
        }

    }

    /**
     * 为单元格设置样式
     * @param sheet 工作表
     * @param cellStyle 单元格样式
     * @param value 单元格值，可为null
     * @param rows 需要填充的行列表
     * @param col 第几列
     */
    public static void setStyle(Sheet sheet, CellStyle cellStyle, String value, List<Integer> rows, int col) {
        for (int r : rows) {
            Row row = sheet.getRow(r);
            if (row == null) {
                row = sheet.createRow(r);
            }

            Cell cell = row.getCell(col - 1);
            if (cell == null) {
                cell = row.createCell(col - 1);
            }
            cell.setCellValue(value);
            cell.setCellStyle(cellStyle);
        }
    }

    /**
     * 设置从第几行第几列开始到第几列结束，行大小直到填充完数据为止
     * @param sheet 工作表
     * @param value 填充值列表
     * @param startRow 开始行
     * @param startCol 开始列
     * @param endCol 结束列
     */
    public static void setCellWithValueList(Sheet sheet, List<String> value, int startRow, int startCol, int endCol) {
        Iterator<String> iter = value.iterator();
        for (int i = startRow - 1; iter.hasNext(); i++) {
            Row row = sheet.getRow(i);
            if (row == null)
                row = sheet.createRow(i);

            for (int j = startCol - 1; j <= endCol - 1; j++) {
                Cell cell = row.getCell(j);
                if (cell == null)
                    cell = row.createCell(j);

                cell.setCellValue(iter.next());
            }
        }
    }
}
